package com.bbm.communitysystem.controller;


import com.bbm.communitysystem.core.domain.R;
import com.bbm.communitysystem.domain.SliderImg;
import com.bbm.communitysystem.domain.bo.SliderImgBo;
import com.bbm.communitysystem.domain.dto.SliderImgDto;
import com.bbm.communitysystem.domain.vo.SliderImgPageVo;
import com.bbm.communitysystem.service.ISliderImgService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bbm
 * @since 2025-02-13
 */
@Tag(name = "轮播图")
@RestController
@RequestMapping("/slider_img")
@RequiredArgsConstructor
public class SliderImgController {

    private final ISliderImgService sliderImgService;

    @Operation(summary = "新增轮播图")
    @PostMapping()
    @Parameters({
            @Parameter(name = "title", description = "标题"),
            @Parameter(name = "pageName", description = "页面名称"),
            @Parameter(name = "href", description = "跳转地址"),
            @Parameter(name = "user", description = "上传者")
    })
    public R<String> addSliderImg(@RequestParam("img") MultipartFile file, @RequestParam("title") String title,
                          @RequestParam("pageName") String pageName, @RequestParam("href") String href,
                                  @RequestParam("user") Integer user) {

        if (file.getOriginalFilename().matches("\\.(png | jpg)")) {
            return R.error("文件格式错误, 只支持png格式和jpg格式的图片");
        }


        SliderImgBo sliderImgBo = new SliderImgBo();
        sliderImgBo.setHref(href);
        sliderImgBo.setTitle(title);
        sliderImgBo.setImg(file);
        sliderImgBo.setUserId(user + "");
        sliderImgBo.setPageName(pageName);

        SliderImgDto sliderImgDto = sliderImgService.addSliderImg(sliderImgBo);

        if (sliderImgDto == null) {
            return R.error("新增失败");
        }

        return R.ok("新增成功");
    }

    @Operation(summary = "根据页面名称获取轮播图")
    @GetMapping("/pageName")
    @Parameters({
            @Parameter(name = "page", description = "页面名称")
    })
    public R<List<SliderImgPageVo>> findSliderImgByPageName(@RequestParam("page") String pageName) {

        List<SliderImg> list = sliderImgService.lambdaQuery()
                .eq(SliderImg::getPageName, pageName)
                .eq(SliderImg::getStatus, 1)
                .list();

        List<SliderImgPageVo> list1 = new ArrayList<>();

        list.forEach(item -> {
                    SliderImgPageVo sliderImgPageVo = new SliderImgPageVo();
                    sliderImgPageVo.setSliderId(item.getId());
                    sliderImgPageVo.setText(item.getTitle());
                    sliderImgPageVo.setHref(item.getHref());
                    sliderImgPageVo.setImg(item.getImgUrl());
                    list1.add(sliderImgPageVo);
                });

        return R.ok("获取成功", list1);
    }


    @Operation(summary = "删除轮播图")
    @DeleteMapping("/{id}")
    @Parameters({
            @Parameter(name = "id", description = "轮播图id"),
            @Parameter(name = "user", description = "删除者id")
    })
    public R<String> delSliderImg(@PathVariable("id") Integer id,
                                  @RequestParam("user") Integer user) {

        SliderImgBo sliderImgBo = new SliderImgBo();

        sliderImgBo.setUserId(user + "");
        sliderImgBo.setId(id);

        SliderImgDto sliderImgDto = sliderImgService.delSliderImg(sliderImgBo);

        if (sliderImgDto == null) {
            return R.error("删除失败");
        }

        return R.ok("删除成功");
    }

    @Operation(summary = "修改轮播图", description = "不能修改图片")
    @PutMapping
    public R putSliderImg(@RequestBody SliderImgDto sliderImgDto) {

        boolean update = sliderImgService.lambdaUpdate()
                .set(!sliderImgDto.getTitle().isEmpty(), SliderImg::getTitle, sliderImgDto.getTitle())
                .set(!sliderImgDto.getHref().isEmpty(), SliderImg::getHref, sliderImgDto.getHref())
                .set(!sliderImgDto.getPageName().isEmpty(), SliderImg::getPageName, sliderImgDto.getPageName())
                .set(SliderImg::getUpdateBy, sliderImgDto.getUserId())
                .set(SliderImg::getUpdateTime, System.currentTimeMillis())
                .set(SliderImg::getStatus, sliderImgDto.getStatus())
                .eq(SliderImg::getId, sliderImgDto.getId())
                .update();

        if (!update) {
            return R.error("修改失败");
        }

        return R.ok("修改成功");
    }

    @Operation(summary = "修改轮播图图片")
    @PostMapping("/put/img")
    @Parameters({
            @Parameter(name = "sliderId", description = "轮播图id"),
            @Parameter(name = "user", description = "上传者")
    })
    public R<String> uploadImg(@RequestParam("img") MultipartFile file, @RequestParam("sliderId") Integer id,
                               @RequestParam("user") Integer user) {

        if (file.getOriginalFilename().matches("\\.(png | jpg)")) {
            return R.error("文件格式错误, 只支持png格式和jpg格式的图片");
        }

        SliderImgBo sliderImgBo = new SliderImgBo();

        sliderImgBo.setId(id);
        sliderImgBo.setImg(file);
        sliderImgBo.setUserId(user + "");

        SliderImgDto sliderImgDto = sliderImgService.putSliderImg(sliderImgBo);

        if (sliderImgDto == null) {
            return R.error("修改失败");
        }

        return R.ok("修改成功");

    }



}
